package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.Consultation;
import com.icss.daos.ConsultationDao;

/**
 * @author 廖鹏
 * @version 创建时间：2010-3-23 下午04:03:37
 * 类说明
 */
@SuppressWarnings("serial")
public class ConsultationServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		
		if("add".equals(action)){
			addConsultation(request,response);
		}
		if("del".equals(action)){
			delConsultationAction(request,response);
		}
		if("list".equals(action)){
			consultationListAction(request,response);
		}
		if("set".equals(action)){
			setPublicAction(request,response);
		}
		
		if("replyConsultation".equals(action)){
			replyConsultationAction(request,response);
		}
		
		if("forwardConsultation".equals(action)){
			forwardConsultationListAction(request,response);
		}
		out.flush();
		out.close();
	}
	
	/**
	 * @author 廖鹏
	 * 功能：用户在线咨询留言
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delConsultationAction(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		
		int consultationID = Integer.parseInt(request.getParameter("consultationID"));
		
		ConsultationDao consultationDao = new ConsultationDao();
		
		boolean b = consultationDao.delConsultation(consultationID);
		
		if(b){
			out.write("<script>alert('删除成功');</script>");
			response.sendRedirect("ConsultationServlet?action=list");
		}
	}
	
	
	private void addConsultation(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		
		String userName = request.getParameter("userName");
		String email = request.getParameter("email");
		String question = request.getParameter("question");

		Consultation consultation = new Consultation();

		consultation.setUserName(userName);
		consultation.setEmail(email);
		consultation.setQuestion(question);

		ConsultationDao consultationDao = new ConsultationDao();

		boolean b = consultationDao.addConsultation(consultation);

		if (b) {
			out.write("<script>alert('留言成功');</script>");
			response.sendRedirect("index.jsp");
		}
	}
	
	
	@SuppressWarnings("unchecked")
	private void consultationListAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		ConsultationDao consultationDao = new ConsultationDao();
		
		List<Consultation> consultationList = consultationDao.queryConsultationsForPage();
		
		request.setAttribute("consultationList", consultationList);
		request.getRequestDispatcher("/page/consultation/consultation_list.jsp").forward(request, response);
		out.flush();
		out.close();
	}
	
	private void forwardConsultationListAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		
		request.getRequestDispatcher("/page/consultation/consultation.jsp").forward(request, response);
		out.flush();
		out.close();
	}
	
	
	private void setPublicAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		int consultationID = Integer.parseInt(request
				.getParameter("consultationID"));
		
		int visiable = Integer.parseInt(request
				.getParameter("visiable"));

		ConsultationDao consultationDao = new ConsultationDao();

		boolean b = consultationDao.updateConsultation(consultationID,visiable);

		if (b) {
			out.write("<script>alert('设置成功');</script>");
			response.sendRedirect("ConsultationServlet?action=list");
		}

	}
	
	private void replyConsultationAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		int consultationID = Integer.parseInt(request
				.getParameter("consultationID"));
		
		String replyText = request.getParameter("replyText");

		ConsultationDao consultationDao = new ConsultationDao();

		boolean b = consultationDao.updateConsultation(consultationID,replyText);

		if (b) {
			out.write("<script>alert('回复成功');</script>");
			response.sendRedirect("ConsultationServlet?action=list");
		}
		out.close();

	}

}
